package com.cc.openeye.find.item.columnCardList

import androidx.compose.runtime.Composable
import assest.common.right_jt
import com.cc.openeye.backgroundColor
import com.cc.openeye.borderRadius
import com.cc.openeye.find.entity.find.Item
import com.cc.openeye.find.entity.find.ItemX
import com.cc.openeye.height
import com.cc.openeye.padding
import com.cc.openeye.width
import com.tencent.kuikly.compose.coil3.rememberAsyncImagePainter
import com.tencent.kuikly.compose.foundation.Image
import com.tencent.kuikly.compose.foundation.layout.Arrangement
import com.tencent.kuikly.compose.foundation.layout.Box
import com.tencent.kuikly.compose.foundation.layout.Column
import com.tencent.kuikly.compose.foundation.layout.Row
import com.tencent.kuikly.compose.foundation.layout.fillMaxWidth
import com.tencent.kuikly.compose.foundation.layout.height
import com.tencent.kuikly.compose.foundation.layout.padding
import com.tencent.kuikly.compose.material3.Text
import com.tencent.kuikly.compose.resources.painterResource
import com.tencent.kuikly.compose.ui.Alignment
import com.tencent.kuikly.compose.ui.Modifier
import com.tencent.kuikly.compose.ui.graphics.Color
import com.tencent.kuikly.compose.ui.layout.ContentScale
import com.tencent.kuikly.compose.ui.text.font.FontWeight
import com.tencent.kuikly.compose.ui.unit.dp
import com.tencent.kuikly.compose.ui.unit.sp

@Composable
fun ColumnCardList(item: Item) {
    Column {
        Box(
            modifier = Modifier.fillMaxWidth().padding(start = 15f, end = 15f).height(50.dp),
            contentAlignment = Alignment.CenterStart
        ) {
            Text(
                "专题策划",
                color = Color(0xff333333),
                fontSize = 16.sp,
                fontWeight = FontWeight.Bold
            )


            Row(
                modifier = Modifier.align(Alignment.CenterEnd),
                verticalAlignment = Alignment.CenterVertically
            ) {
                Text(
                    "查看全部",
                    color = Color(0xff4687d7),
                    fontSize = 13.sp,
                )

                Image(
                    painter = painterResource(right_jt),
                    contentDescription = "蓝色箭头",
                    modifier = Modifier
                        .padding(start = 2.dp).width(12f).height(12f),
                    contentScale = ContentScale.FillBounds
                )
            }

        }

        AdaptiveGrid(
            items = item.data?.itemList ?: emptyList(),
            columns = 2,
            modifier = Modifier.padding(start = 15.dp, end = 15.dp),
        ) { dataItem ->

            Box(
                contentAlignment = Alignment.Center,
            ) {
                Image(
                    modifier = Modifier.fillMaxWidth().height(90f)
                        .backgroundColor(Color(0xfff5f5f5)).borderRadius(5.dp),
                    painter = rememberAsyncImagePainter(dataItem.data.image),
                    contentDescription = "",
                )

                Text(dataItem.data.description)
            }
        }

    }
}

@Composable
fun AdaptiveGrid(
    items: List<ItemX>,
    columns: Int,
    modifier: Modifier = Modifier,
    content: @Composable (ItemX) -> Unit
) {
    val rows = (items.size + columns - 1) / columns
    Column(modifier = modifier, verticalArrangement = Arrangement.spacedBy(5.dp)) {
        for (rowIndex in 0 until rows) {
            val startIndex = rowIndex * columns
            val endIndex = minOf(startIndex + columns, items.size)
            val currentRowItems = items.subList(startIndex, endIndex)
            Row(horizontalArrangement = Arrangement.spacedBy(5.dp)) {
                for (item in currentRowItems) {
                    Box(
                        modifier = Modifier.weight(1f)
                    ) {
                        content(item)
                    }
                }
                // 如果当前行不够列数，用空的Box填充保持对齐
                if (currentRowItems.size < columns) {
                    for (i in currentRowItems.size until columns) {
                        Box(modifier = Modifier.weight(1f)) {}
                    }
                }
            }
        }
    }
}